﻿Public Class FdapProjectKeyClass

    ''' <summary>
    ''' 从数据库中查询指定键的值
    ''' </summary>
    ''' <param name="key"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetValue(ByVal pid As String, ByVal key As String) As String

        Dim strSql As String
        strSql = String.Format("Select `value` From fdapprojectinfo Where projectID='{0}' and `key`='{1}'", pid, key.Replace("'", "''").Replace("--", "__"))
        Dim obj As Object
        obj = TBCC.UtilityMobile.SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return String.Empty
        End If
        Return Convert.ToString(obj)
    End Function


    Public Shared Sub SetValue(ByVal pid As String, ByVal key As String, ByVal value As String)
        CheckKey(pid, key)
        value = value.Replace("\", "/")
        Dim strSql As String
        strSql = String.Format("Update fdapprojectinfo set `value`='{0}' Where projectID='{1}' and `key`='{2}'", value.Replace("'", "''").Replace("--", "__"), pid, key.Replace("'", "''").Replace("--", "__"))
        TBCC.UtilityMobile.SqlHelper.ExecuteNonQuery(strSql, Nothing)
    End Sub


    Public Shared Sub CheckKey(ByVal pid As String, ByVal key As String)


        Dim strSql As String
        strSql = "Select count(*) From fdapprojectinfo where projectID=?p0 and `key`=?p1"
        Dim p(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p(0).ParameterName = "p0" : p(0).Value = pid
        p(1).ParameterName = "p1" : p(1).Value = key

        Dim obj As Object
        obj = TBCC.UtilityMobile.SqlHelper.ExecuteScalar(strSql, p)

        If 1 = obj Then
            Exit Sub
        End If

        strSql = "Insert Into fdapprojectinfo(projectID,`key`,`value`) values(?p0,?p1,?p2)"
        Dim p2(2) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p2.Length - 1
            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next

        p2(0).ParameterName = "p0" : p2(0).Value = pid
        p2(1).ParameterName = "p1" : p2(1).Value = key
        p2(2).ParameterName = "p2" : p2(2).Value = ""


        TBCC.UtilityMobile.SqlHelper.ExecuteNonQuery(strSql, p2)

    End Sub

    ''' <summary>
    ''' 从数据库中查询指定键的值
    ''' </summary>
    ''' <param name="key"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetValue1(ByVal key As String) As String

        Dim strSql As String
        strSql = String.Format("Select `value` From fdapprojectinfo Where  `key`='{0}'", key.Replace("'", "''").Replace("--", "__"))
        Dim obj As Object
        obj = TBCC.UtilityMobile.SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return String.Empty
        End If
        Return Convert.ToString(obj)
    End Function


    Public Shared Sub SetValue1(ByVal key As String, ByVal value As String)
        CheckKey1(key)
        value = value.Replace("\", "/")
        Dim strSql As String
        strSql = String.Format("Update fdapprojectinfo set `value`='{0}' Where  `key`='{1}'", value.Replace("'", "''").Replace("--", "__"), key.Replace("'", "''").Replace("--", "__"))
        TBCC.UtilityMobile.SqlHelper.ExecuteNonQuery(strSql, Nothing)
    End Sub


    Public Shared Sub CheckKey1(ByVal key As String)


        Dim strSql As String
        strSql = "Select count(*) From fdapprojectinfo where `key`=?p0"
        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p.Length - 1
            p(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next
        p(0).ParameterName = "p0" : p(0).Value = key

        Dim obj As Object
        obj = TBCC.UtilityMobile.SqlHelper.ExecuteScalar(strSql, p)

        If 1 = obj Then
            Exit Sub
        End If

        strSql = "Insert Into fdapprojectinfo(`key`,`value`) values(?p0,?p1)"
        Dim p2(1) As MySql.Data.MySqlClient.MySqlParameter
        For i As Integer = 0 To p2.Length - 1
            p2(i) = New MySql.Data.MySqlClient.MySqlParameter()
        Next


        p2(0).ParameterName = "p0" : p2(0).Value = key
        p2(1).ParameterName = "p1" : p2(1).Value = ""


        TBCC.UtilityMobile.SqlHelper.ExecuteNonQuery(strSql, p2)

    End Sub

End Class
